/**
 * 
 */
package cn.tonyqiu.wonuly.client.gui;

import java.util.List;

import cn.tonyqiu.wonuly.client.i18n.WonulyConstants;
import cn.tonyqiu.wonuly.client.rpc.RPCGenerator;
import cn.tonyqiu.wonuly.client.vo.BillVo;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TableListener;
import com.google.gwt.user.client.ui.Widget;

/**
 * A composite that add the content, include bill, category and the schedule
 * 
 * @author <a href="mailto:tony.qiu.smart@gmail.com">Tony Qiu</a>
 * 
 */
public class BillContentList extends Composite implements TableListener,
		ClickListener {

	private WonulyConstants constants = Wonuly.constants;

	private static final int VISIBLE_COUNT = 10;

	private HTML countLabel = new HTML();
	private HTML previousButton = new HTML("<a href='javascript:;'>&lt; "
			+ constants.contentListPreviousPage() + "</a>", true);
	private HTML nextButton = new HTML("<a href='javascript:;'>"
			+ constants.contentListNextPage() + "&gt;</a>", true);
	private int startIndex, selectedRow = -1;
	private FlexTable table = new FlexTable();
	private HorizontalPanel navBar = new HorizontalPanel();

	public BillContentList() {
		// Setup the table.
		table.setCellSpacing(0);
		table.setCellPadding(0);
		table.setWidth("100%");

		// Hook up events.
		table.addTableListener(this);
		previousButton.addClickListener(this);
		nextButton.addClickListener(this);

		// Create the 'navigation' bar at the upper-right.
		HorizontalPanel innerNavBar = new HorizontalPanel();
		navBar.setStyleName("mail-ListNavBar");
		innerNavBar.add(previousButton);
		innerNavBar.add(countLabel);
		innerNavBar.add(nextButton);

		navBar.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
		navBar.add(innerNavBar);
		navBar.setWidth("100%");

		initWidget(table);
		setStyleName("mail-List");

		initBillTable();
		update();
	}

	public void onCellClicked(SourcesTableEvents sender, int row, int cell) {
		// Select the row that was clicked (-1 to account for header row).
		if (row > 0) {
			// selectRow(row - 1);
			styleRow(selectedRow, false);
			styleRow(row - 1, true);
			selectedRow = row - 1;
			Window.alert(get(row).getId());
		}
	}

	public void onClick(Widget sender) {
		if (sender == nextButton) {

		} else if (sender == previousButton) {
			// Move back a page.
			startIndex -= VISIBLE_COUNT;
			if (startIndex < 0) {
				startIndex = 0;
			} else {
				styleRow(selectedRow, false);
				selectedRow = -1;
				update();
			}
		}
	}

	/**
	 * Initializes the Bill table so that it contains enough rows for a full
	 * page of bills. Also creates the images that will be used as 'read' flags.
	 */
	private void initBillTable() {
		// Create the header row.
		table.setText(0, 0, constants.contentListBillId());
		table.setText(0, 1, constants.contentListBillCategory());
		table.setText(0, 2, constants.contentListBillAmount());
		table.setText(0, 3, constants.contentListBillDate());
		table.setText(0, 4, constants.contentListBillComment());
		table.setWidget(0, 5, navBar);
		table.getRowFormatter().setStyleName(0, "mail-ListHeader");
	}

	private void styleRow(int row, boolean selected) {
		if (row != -1) {
			if (selected) {
				table.getRowFormatter().addStyleName(row + 1,
						"mail-SelectedRow");
			} else {
				table.getRowFormatter().removeStyleName(row + 1,
						"mail-SelectedRow");
			}
		}
	}

	/**
	 * update function, it will invoke different update function base on the
	 * ListType
	 */
	private void update() {
		RPCGenerator.getBillAsync().getBillsByUser(
				new AsyncCallback<List<BillVo>>() {

					public void onFailure(Throwable arg0) {
						Window.alert("Exception:" + arg0);

					}

					public void onSuccess(List<BillVo> bills) {
						for (int i = 0; i < bills.size(); i++) {
							set(i, bills.get(i));
						}

					}

				});
	}

	public void set(int i, BillVo bill) {
		table.setText(i, 0, bill.getId());
		table.setText(i, 1, bill.getCategory());
		table.setText(i, 2, bill.getAmount());
		table.setText(i, 3, bill.getDate());
		table.setText(i, 4, bill.getComment());
		table.getCellFormatter().setWordWrap(i + 1, 0, false);
		table.getCellFormatter().setWordWrap(i + 1, 1, false);
		table.getCellFormatter().setWordWrap(i + 1, 2, false);
		table.getCellFormatter().setWordWrap(i + 1, 3, false);
		table.getCellFormatter().setWordWrap(i + 1, 4, false);
		table.getFlexCellFormatter().setColSpan(i + 1, 4, 2);
	}

	public BillVo get(int i) {
		BillVo bill = new BillVo();
		bill.setId(table.getText(i, 0));
		bill.setCategory(table.getText(i, 1));
		bill.setAmount(table.getText(i, 2));
		bill.setDate(table.getText(i, 3));
		bill.setComment(table.getText(i, 4));
		return bill;
	}
}
